﻿using Entity;
using Reopsitory.Menus;
using Reopsitory.RoleInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.RoleInfos
{
    public class RoleService : IRoleService
    {
        private readonly IRoleReopsitory roleReopsitory;
        private readonly IMenuRepository menuReopsitory;
        private readonly IRoleMenuReopsitory roleMenuReopsitory;

        public RoleService(IRoleReopsitory roleReopsitory, IMenuRepository menuReopsitory,IRoleMenuReopsitory roleMenuReopsitory)
        {
            this.roleReopsitory = roleReopsitory;
            this.menuReopsitory = menuReopsitory;
            this.roleMenuReopsitory = roleMenuReopsitory;
        }

        public RestFull<RoleDto> Query()
        {
            var list = roleReopsitory.Queryable().Select(x => new RoleDto
            {
                RoleId = x.RoleId,
                RoleName = x.RoleName,
            }).ToList();

            return new RestFull<RoleDto> { Data = list };
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name = "menuDto" ></ param >
        /// < returns ></ returns >
        public RestFull<RoleDto> Insert(RoleDto roleDto)
        {
            RoleInfo role = new RoleInfo();
            role.RoleName = roleDto.RoleName;

            roleReopsitory.Insert(role);

            return new RestFull<RoleDto> { Code = 1, Message = "添加成功" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RestFull Delete(int id)
        {
            var data = roleReopsitory.QueryFirst(id);

            roleReopsitory.Delete(data.RoleId);

            return new RestFull { Code = 1, Message = "删除成功" };
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RestFull QueryFirst(int id)
        {
            var data = roleReopsitory.QueryFirst(id);
            return new RestFull { Data = data };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name = "menuDto" ></ param >
        /// < returns ></ returns >
        public RestFull<RoleDto> Update(RoleDto roleDto)
        {
            RoleInfo role = new RoleInfo();
            role.RoleName = roleDto.RoleName;
            role.RoleId = roleDto.RoleId;

            roleReopsitory.Update(role);

            return new RestFull<RoleDto> { Code = 1, Message = "修改成功" };
        }
    }
}
